package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtil;
import org.apache.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(timeout = 60000)
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {


    /**
     * 导入数据
     * @param data   文件信息  文件名
     * @return
     */
    @Override
    public boolean importData(List<String[]> data) {

        List<OrderSetting> list = new ArrayList<>();

        //遍历文件信息
        for (String[] row : data) {
            //获取第一列
            String orderData = row[0];
            //获取第二列
            Integer number = Integer.parseInt(row[1]);

            //判断日期对应的设置是否存在
            OrderSetting orderSetting = findByData(orderData);
            //如果不存在 将数据封装到orderSetting
            if (orderSetting==null){
                orderSetting = new OrderSetting();
                orderSetting.setOrderDate(DateUtil.parseDate(orderData));
            }
            orderSetting.setNumber(number);

            list.add(orderSetting);
        }
        //批量插入  提高性能
        this.saveOrUpdateBatch(list);
        return true;
    }


    //查询数据库中是否存在相对应的日期
    @Override
    public OrderSetting findByData(String orderData) {
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("order_date",orderData);

        return this.getOne(queryWrapper);
    }


    //通过年月查询
    @Override
    public Map selectByYearMonth(int year, int mouth) {

        Map map = new HashMap<>();

        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        //select t.* ,date_format(order_date,"%Y-%m") from t_order_setting t
        //where date_format(order,"%Y-%m") = '2020-11'

        queryWrapper.eq("date_format(order_date,'%Y-%m')",
                year + "-"+ (mouth<10?"0":"")+mouth);

        //      settingData: {
        //        '2020-11-01':{number:200,reservations:200},
        //        '2020-11-02':{number:200,reservations:100},
        //        '2020-11-15':{number:200,reservations:0}
        //      },
        List<OrderSetting> dataList = this.list(queryWrapper);
        for (OrderSetting orderSetting : dataList) {
            String orderDate = DateUtil.formatDate(orderSetting.getOrderDate());

            map.put(orderDate,orderSetting);
        }
        return map;
    }


    //页面自定义设置单元格
    @Override
    public boolean settingDate(String day, int num) {
        //判断日期对应的设置是否存在
        OrderSetting orderSetting = findByData(day);
        if (orderSetting!=null){
            orderSetting = new OrderSetting();
            orderSetting.setOrderDate(DateUtil.parseDate(day));
        }
        orderSetting.setNumber(num);
        return this.saveOrUpdate(orderSetting);
    }


}
